<template>
  <div class="container">
    <el-card>
      <!-- 顶部区域 -->
      <div class="title-box">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="学科名称">
            <el-input
              v-model="getListObj.subjectName"
              style="width: 200px; margin-right: 10px"
            ></el-input>
            <el-button @click="resetForm">清除</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-button
          type="success"
          icon="el-icon-edit"
          class="addBtn"
          @click="add"
          >新增学科</el-button
        >
      </div>
      <el-alert
        :title="title"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 10px;"
      >
      </el-alert>
      <!-- <el-alert :title="title" type="info" show-icon> </el-alert> -->
      <!-- 表格数据区域 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="200">
          <template slot-scope="scope"
            >{{ scope.row.addDate | formate }}
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="140">
          <template slot-scope="scope">
            {{ scope.row.isFrontDisplay === 1 ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="280px">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="$router.push(`/subjects/directorys?id=${scope.row.id}`)"
              >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="
                $router.push(
                  `/subjects/tags?id=${scope.row.id}&name=${scope.row.subjectName}`
                )
              "
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="edit(scope.row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="remove(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getListObj.page"
        :page-sizes="[3, 5, 10, 12]"
        :page-size="getListObj.pagesize"
        layout=" prev, pager, next, sizes, jumper"
        :total="total"
        style="text-align: right"
      >
      </el-pagination>
    </el-card>
    <!-- 弹出对话框 -->
    <subjectsDia
      :dialogFormVisible.sync="dialogFormVisible"
      @success="diaGetList"
      :isEdit="isEdit"
      :dataEdit="dataEdit"
      v-if="dialogFormVisible"
    ></subjectsDia>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import subjectsDia from './subjectsDia.vue'
import { list, remove } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      dataEdit: {},
      isEdit: false,
      dialogFormVisible: false,
      form: {
        name: ''
      },
      getListObj: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      tableData: [],
      total: 0
    }
  },
  components: { subjectsDia },
  filters: {
    formate: function (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    this.getList()
  },
  computed: {
    title () {
      return `数据一共${this.total}条`
    }
  },
  methods: {
    // getItem () {
    //   const row = document.querySelector('.el-table__row')
    //   console.log(row)
    // },
    // // 给每个行加入动画
    // tableClassName ({ row, rowIndex }) {
    //   return 'wz'
    // },
    diaGetList () {
      this.getList()
    },
    // 去往学科分类页面
    toDirectorys (row) {
      console.log(row)
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 新增
    add () {
      this.isEdit = false
      this.dialogFormVisible = true
      this.dataEdit = {
        subjectName: '',
        isFrontDisplay: 1
      }
    },
    // 修改
    edit (row) {
      this.isEdit = true
      this.dataEdit = row
      this.dialogFormVisible = true
    },
    // 删除
    remove (data) {
      this.$confirm('此操作将永久删除该学科目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (this.tableData.length === 1) {
            this.getListObj.page--
          }
          await remove(data)

          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 表单提交
    onSubmit () {
      this.getList()
    },
    resetForm () {
      this.getListObj = {
        subjectName: ''
      }
    },
    // 获取学科列表
    async getList () {
      const res = await list(this.getListObj)
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    // 分页
    handleSizeChange (val) {
      this.getListObj.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.getListObj.page = val
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.addBtn {
  margin-bottom: 25px;
}
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
